#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys

from typing import Annotated
from uuid import uuid4

from fastapi import Depends
from sqlalchemy import URL
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.ext.declarative import declarative_base
from common.log import log
# from common.model import MappedBase
from core.conf import settings


def create_engine_and_session(url: str | URL):
    try:
        # 数据库引擎
        engine = create_async_engine(url, echo=settings.SQL_ECHO, future=True, pool_pre_ping=True)
        log.success('数据库连接成功')
    except Exception as e:
        log.error('❌ 数据库链接失败 {}', e)
        sys.exit()
    else:
        db_session = async_sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)
        return engine, db_session


SQL_URLS = {
    'mysql': (
        f'mysql+asyncmy://{settings.SQL_USER}:{settings.SQL_PASSWORD}@{settings.SQL_HOST}:'
        f'{settings.SQL_PORT}/{settings.SQL_DATABASE}?charset={settings.SQL_CHARSET}'
    ),
    'postgresql': (
        f'postgresql+asyncpg://{settings.SQL_USER}:{settings.SQL_PASSWORD}@{settings.SQL_HOST}:'
        f'{settings.SQL_PORT}/{settings.SQL_DATABASE}'
    )
}

SQLALCHEMY_DATABASE_URL = SQL_URLS.get(settings.SQL_TYPE)
async_engine, async_db_session = create_engine_and_session(SQLALCHEMY_DATABASE_URL)

Base = declarative_base()


async def get_db() -> AsyncSession:
    """session 生成器"""
    session = async_db_session()
    try:
        yield session
    except Exception as se:
        await session.rollback()
        raise se
    finally:
        await session.close()


# Session Annotated
SQLSession = Annotated[AsyncSession, Depends(get_db)]


def get_session():
    return SQLSession


# async def create_table():
#     """创建数据库表"""
#     async with async_engine.begin() as coon:
#         await coon.run_sync(MappedBase.metadata.create_all)


def uuid4_str() -> str:
    """数据库引擎 UUID 类型兼容性解决方案"""
    return str(uuid4())
